//
//  HWGameClassifyHttpManager.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/3/3.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import Alamofire
class HWGameCenterHttpManager: NSObject {
    var uid = HWLocallyStoredData.getLoginUserID()
    var tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
    var p = 1
    var max_p = "1"
}

extension HWGameCenterHttpManager {
    /// sign加密
    /// - Returns: sign
    func encrySign() -> String {
        
        let sign = "\(uid)\(tStr)\("fsd213ewdsadqwe2121213edsad")"
        let signMD5 = WMUtil.md5HexDigest(sign) ?? ""
        return signMD5
    }
    func headers() -> HTTPHeaders {
        //        let tStr = Date().timeStamp
        //        let signature = "nnmitu"+tStr
        //
        //        let json = ["usersid":HWLocallyData.getUsersID(),"timestamp":tStr,"signature":signature.DDMD5Encrypt(.lowercase32)]
        //        let jsonString = HWPublicFuncTools.convertDictionAryToJson(dic: json as NSDictionary)
        //        let aesStr:String = aesEncryptString(jsonString, "NrrynIXcpucYvGSk")
        let header:HTTPHeaders = ["eaua":JJUUIDManager.getUUID(),"imei":ASIdentifierManager.shared().advertisingIdentifier.uuidString]
        
        return header
    }
}

extension HWGameCenterHttpManager {
    /// 分类
    /// - Parameters:
    ///   - success: model
    ///   - failure: error
    func getGameClassifyConfig(success:@escaping (_ models:[HWGameClassifyConfigModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "member_id":HWLocallyStoredData.getCode(),
            "ios":"1"
        ] as [String : Any]
        
        AF.request(URL(string: URL_GameClassify)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGameClassifyConfigModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWGameClassifyConfigModel])
                            
                        }
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    /// 获取分类游戏列表
    /// - Parameters:
    ///   - success: model
    ///   - failure: error
    func getClassifyGameList(typeId:String,success:@escaping (_ models:HWClassifyModel) -> Void, failure:@escaping(_ errorString:String)->Void){
        p = 1
        let parameters  = [
            "fenlei_id":typeId,
            "type":typeId,
            "p":p,
            "ios":"1",
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GetClassifyGameList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = HWClassifyModel.deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model)
                            
                        }
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    /// 获取分类游戏列表
    /// - Parameters:
    ///   - success: model
    ///   - failure: error
    func getClassifyGameListFooter(typeId:String,success:@escaping (_ model:HWClassifyModel) -> Void, failure:@escaping(_ errorString:String)->Void){
        p += 1
        let parameters  = [
            "fenlei_id":typeId,
            "type":typeId,
            "p":p,
            "ios":"1",
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GetClassifyGameList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = HWClassifyModel.deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model)
                            
                        }
                        
                    } else if resultDic["code"] as? Int == 100 {
                        self.p -= 1
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        self.p -= 1
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    self.p -= 1
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                self.p -= 1
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    /// 游戏榜分类
    /// - Parameters:
    ///   - success: model
    ///   - failure: error
    func getGameRankListClassifyConfig(success:@escaping (_ models:[HWGameClassifyConfigModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        tStr = "\(WMUtil.getTimeSp())\(WMUtil.getRandomNumber(101, to: 998))"
        uid = HWLocallyStoredData.getLoginUserID()
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "member_id":HWLocallyStoredData.getCode(),
            "ios":"1",
        ] as [String : Any]
        
        AF.request(URL(string: URL_GameRankListClassify)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGameClassifyConfigModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWGameClassifyConfigModel])
                            
                        }
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    
    /// 获取游戏排行列表
    func getGameRankList(t:String,typeId:String,success:@escaping (_ models:[HWGameRankListModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        p = 1
        let parameters  = [
            "t":t,
            "type":typeId,
            "p":p,
            "ios":"1",
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GameRankList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGameRankListModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWGameRankListModel])
                            
                        }
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    /// 获取游戏排行列表
    /// - Parameters:
    ///   - success: model
    ///   - failure: error
    func getGameRankListFooter(t:String,typeId:String,success:@escaping (_ models:[HWGameRankListModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        p += 1
        let parameters  = [
            "t":t,
            "type":typeId,
            "p":p,
            "ios":"1",
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GameRankList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGameRankListModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWGameRankListModel])
                            
                        }
                        
                    } else if resultDic["code"] as? Int == 100 {
                        self.p -= 1
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        self.p -= 1
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    self.p -= 1
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                self.p -= 1
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    
    
    /// 获取开服列表
    func getOpenServiceList(type:String,success:@escaping (_ models:[HWGameOpenServiceListModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        p = 1
        let parameters  = [
            "type":type,
            "p":p,
            "ios":"1",
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GameOpenServiceList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGameOpenServiceListModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWGameOpenServiceListModel])
                            
                        }
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    /// 获取开服列表
    /// - Parameters:
    ///   - success: model
    ///   - failure: error
    func getOpenServiceListFooter(type:String,success:@escaping (_ models:[HWGameOpenServiceListModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        p += 1
        let parameters  = [
            "type":type,
            "p":p,
            "ios":"1",
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GameOpenServiceList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWGameOpenServiceListModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWGameOpenServiceListModel])
                            
                        }
                        
                    } else if resultDic["code"] as? Int == 100 {
                        self.p -= 1
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        self.p -= 1
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    self.p -= 1
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                self.p -= 1
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
   
    
    /// 获取预约列表
    func getAppointmentList(success:@escaping (_ models:[HWAppointmentRankModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        p = 1
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "p":p,
            "ios":"1",
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GameAppointmentList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWAppointmentRankModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWAppointmentRankModel])
                            
                        }else {
                            failure(resultDic["message"] as? String ?? "")
                        }
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    /// 获取预约列表
    /// - Parameters:
    ///   - success: model
    ///   - failure: error
    func getAppointmentListFooter(success:@escaping (_ models:[HWAppointmentRankModel]) -> Void, failure:@escaping(_ errorString:String)->Void){
        p += 1
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "p":p,
            "ios":"1",
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_GameAppointmentList)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        
                        print("***********\(HWUniversal.JSONStringToDictionary(jsonString: result))")
                        if let model = [HWAppointmentRankModel].deserialize(from: result ,designatedPath: ".data") {
                            print(model)
                            success(model as! [HWAppointmentRankModel])
                            
                        }
                        
                    } else if resultDic["code"] as? Int == 100 {
                        self.p -= 1
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        self.p -= 1
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    self.p -= 1
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                self.p -= 1
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
    
    /// 添加预约
    func getAddAppointment(gameId:String,success:@escaping (_ success:Bool) -> Void, failure:@escaping(_ errorString:String)->Void){
        p = 1
        let parameters  = [
            "uid":uid,
            "t":tStr,
            "sign":encrySign(),
            "gid":gameId,
            "ios":"1",
            "member_id":HWLocallyStoredData.getCode()
        ] as [String : Any]
        
        AF.request(URL(string: URL_AddAppointment)!, method: .post, parameters: parameters, headers: headers()).responseString { (responses) in
            if responses.value != nil {
                
                let result:String = responses.value ?? ""
                if result != "" {
                    
                    let resultDic = HWUniversal.JSONStringToDictionary(jsonString: result)
                    if resultDic["code"] as? Int == 1 {
                        
                        success(true)
                        
                    } else if resultDic["code"] as? String == "100" {
                        HWUniversal.reLogin(message: (resultDic["message"] as? String) ?? "")
                    }else {
                        
                        print("请求失败--原因:\(resultDic["message"] ?? "请求失败--未知原因")")
                        failure(resultDic["message"] as? String ?? "")
                    }
                    
                }else {
                    failure("没有获取到数据")
                    print("没有获取到数据")
                    
                }
                
            }else {
                failure("获取数据失败")
                print("获取数据失败")
                //                self.p -= 1
            }
        }
    }
}
